﻿using QWManage.DAL;
using QWManage.Model;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace QWManage.Forms
{
    public partial class FrmCustomersOfGroup : Form
    {
        private string GroupID { set; get; }
        public FrmCustomersOfGroup(string groupID)
        {
            GroupID = groupID;
            InitializeComponent();
        }
        private void loadData()
        {
            lvCustomers.Items.Clear();
            CustomersGroupsDAL customersGroupsDAL = new CustomersGroupsDAL();
            List<CustomerGroupInfo> customerGroupInfos = customersGroupsDAL.GetCustomerGroupInfosByGroupID(GroupID);
            CustomerDAL customerDAL = new CustomerDAL();

            int notInGroup = 0;
            string keywords = txtKeywords.Text.ToLower().Trim();
            foreach (CustomerGroupInfo customerGroupInfo in customerGroupInfos)
            {
                bool matched = false;
                if (keywords != "")
                {
                    if (customerGroupInfo.UserID != null &&
                        customerGroupInfo.UserID.ToLower().Contains(keywords))
                        matched = true;
                    else if (customerGroupInfo.Name != null &&
                        customerGroupInfo.Name.ToLower().Contains(keywords))
                        matched = true;

                    if (!matched) continue;
                }

                ListViewItem item = new ListViewItem(customerGroupInfo.UserID);
                item.Tag = customerGroupInfo;
                item.SubItems.Add(customerGroupInfo.Name);
                item.SubItems.Add(customerGroupInfo.Status ? "在群" : "不在群");

                if (customerGroupInfo.Type.HasValue)
                    item.SubItems.Add(customerGroupInfo.Type.ToString());
                else
                    item.SubItems.Add("");

                if (customerGroupInfo.JoinTime.HasValue)
                {
                    DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(customerGroupInfo.JoinTime.Value);
                    DateTime dateTime = dateTimeOffset.LocalDateTime;
                    item.SubItems.Add(dateTime.ToString("yyyy年MM月dd日 HH:mm:ss"));
                }
                else
                    item.SubItems.Add("");

                if (customerGroupInfo.JoinScene.HasValue)
                    item.SubItems.Add(customerGroupInfo.JoinScene.ToString());
                else item.SubItems.Add("");

                item.SubItems.Add(customerGroupInfo.InvitorUserID);
                item.SubItems.Add(customerGroupInfo.GroupNickname);

                if (!customerGroupInfo.Status)
                {
                    item.ForeColor = Color.Red;
                    notInGroup++;
                }
                lvCustomers.Items.Add(item);
            }
            lblStatus.Text = $"共 {Utils.Common.FormatInt32(lvCustomers.Items.Count)} 条记录，不在群 {Utils.Common.FormatInt32(notInGroup)} 条记录";

        }
        private void FrmCustomersOfGroup_Load(object sender, EventArgs e)
        {
            lblStatus.Text = string.Empty;
            lblStatus.Text = string.Empty;

            lvCustomers.View = View.Details;
            lvCustomers.MultiSelect = true;
            lvCustomers.FullRowSelect = true;
            lvCustomers.Columns.Add(new ColumnHeader() { Text = "客户ID", Width = 200 });
            lvCustomers.Columns.Add(new ColumnHeader() { Text = "客户名称", Width = 100 });
            lvCustomers.Columns.Add(new ColumnHeader() { Text = "状态", Width = 100 });

            lvCustomers.Columns.Add(new ColumnHeader() { Text = "Type", Width = 100 });
            lvCustomers.Columns.Add(new ColumnHeader() { Text = "JoinTime", Width = 150 });
            lvCustomers.Columns.Add(new ColumnHeader() { Text = "JoinScene", Width = 100 });
            lvCustomers.Columns.Add(new ColumnHeader() { Text = "InvitorUserID", Width = 100 });
            lvCustomers.Columns.Add(new ColumnHeader() { Text = "GroupNickname", Width = 100 });


            lvCustomers.MouseDoubleClick += LvCustomers_MouseDoubleClick;

            txtKeywords.KeyUp += (s, es) =>
            {
                if (es.KeyCode == Keys.Enter) loadData();
            };

            GroupDAL groupDAL = new GroupDAL();
            GroupInfo groupInfo = groupDAL.GetGroupChatInfo(GroupID);
            if (groupInfo != null) Text = "群名称：" + groupInfo.Name + " [" + GroupID + "]";

            loadData();
        }

        private void LvCustomers_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListViewItem item = lvCustomers.GetItemAt(e.X, e.Y);
            if (item == null) return;
            CustomerGroupInfo customerInfo = item.Tag as CustomerGroupInfo;
            FrmGroupsOfCustomer frm = new FrmGroupsOfCustomer(customerInfo.UserID);
            frm.ShowDialog();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            loadData();
        }
    }
}
